<?php
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Document\Controller;
use \Common\Controller\BaseController;
define('MODULE_CACHE', DATA_PATH.'caches_model/');
Class YardFactoryController extends BaseController {
    public function _initialize() {
    	parent::_initialize();
        $this->categorys = getcache('category', 'commons');
        $this->modelid = 2;
        $this->models = getcache('model', 'commons');
        $this->userid = (int) cookie('_userid');
        /* 将页码变量赋值到模板 */
    }

    /* 前台领取工号 */
    public function chk_yard($yard_id = 0) {
        $aid = (int) $yard_id;       
        if($this->userid < 1) {
            $this->error('请尚未登陆，请登陆后领取工号');
        }
        $model = $this->models[$this->modelid];
        $system_info = M($model['tablename'])->getById($aid);
        if($aid < 1 || !$system_info) $this->error('参数错误');
        $account_map = array(
            'uid'   => $this->userid,
            'aid'   => $aid
        );
        if(!M('DamaAccount')->where($account_map)->count()) {
            $account_info = $account_map;
            $account_info['name'] = 'JSZ'.$this->userid;
            $account_info['inputtime'] = $account_info['updatetime'] = NOW_TIME;
            M('DamaAccount')->add($account_info);
        }
        $info = M('DamaAccount')->where($account_map)->find();
        $this->success('亲，您领取的工号为：'.$info['name'].'，赶快下载软件赚取奖励吧！');
    }

    /* 前台领取奖励 */
    public function do_yard($logid = 0) {
        $logid = (int) $logid;
        if($logid < 1) $this->error('参数错误');
        $uid = (int) cookie('_userid');
        if($uid < 1) $this->error('您尚未登陆，请登陆后领取');
        $log_info = D('DamaLog')->getById($logid);
        if(!$log_info) {
            $this->error('奖励数据不存在');
        }
        if ($log_info['uid'] != $uid) {
            $this->error('您没有权限领取该奖励');
        }
        if ($log_info['status'] == 1) {
            $this->error('该奖励已被领取，请勿重复领取哟~');
        }
        /* 给用户增加U币、写入记录等 */
        if($log_info['yard_u_coin'] > 0) {
            M('Member')->where(array("userid" => $uid))->setInc("u_coin", $log_info['yard_u_coin']);
            $account_info = M('DamaAccount')->getById($log_info['account_id']);
            $model = $this->models[$this->modelid];
            $document_info = M($model['tablename'])->getById($log_info['aid']);
            $message = "领取打码任务[".$document_info['title']." ".dgmdate($log_info['yard_time'], 'Y-m-d')."]，".$log_info['yard_num']."码工资 ，工号：".$account_info['name'];
            score_change($uid, $message, $log_info['yard_u_coin'], 'u_coin', "1");
        }
        $result = D('DamaLog')->where(array("id" => $logid))->setField('status','1');
        if (!$result) {
            $this->error('工资领取失败，请稍候重试');
        }
        $this->success('您已成功领取该日打码工资');
    }
}